<template>
  <div class="SBLIndexSetting">
    <el-row>
      <el-col :span="24">
        <dynForm
          :info-form="infoForm"
          :search-tags="searchTags"
          :select-list="selectList"
        >
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t('search') }}</el-button>
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <!-- v-btnPermission:add -->
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="doAdd">
        {{ $t('add') }}</el-button>
      <!-- v-btnPermission:edit -->
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="handleEdit">
        {{ $t('modify') }}</el-button>
      <!-- v-btnPermission:delete -->
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="handleDelete"
      >{{ $t('delete') }}</el-button>
      <el-button
        type="primary"
        icon="el-icon-download"
        @click="handleDownloadTemp"
      >{{ $t('downloadTemp') }}</el-button>
      <el-upload
        :action="baseApi + '/tdm/upload/submit?name=SBLSettingExcelList'"
        :headers="headers"
        :show-file-list="false"
        :on-success="uploadSuccess"
        :on-error="importFail"
        class="upload-btn"
        style="display: inline-block"
        accept=".xlsx"
      >
        <el-button
          type="primary"
          icon="el-icon-upload"
        >{{ $t("import") }}</el-button
        >
      </el-upload>
    </div>
    <el-card shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <pagination
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        :total="total"
        @pagination="onChangePage"
      />
    </el-card>
    <indexSetting ref="indexSetting" @initList="initList" />
  </div>
</template>
<script>
import { getList, doDelete, download, getPackageTypeList } from '@/api/auto-analyze-warning/SBLIndexSetting'
import { mapGetters } from 'vuex'
import networkTips from '@/mixins/networkTips'
import mySetting from '@/mixins/mySetting'
import indexSetting from './componetns/TheIndexSetting.vue'
import { getToken } from '@/utils/auth'
export default {
  name: 'SBLIndexSetting',
  components: { indexSetting },
  mixins: [mySetting, networkTips],
  data() {
    const searchTags = [
      { label: this.$t('processSpecificationNumber'), prop: 'processSpecificationNo', tag: 'DynInput' },
      { label: this.$t('productName'), prop: 'prodName', tag: 'DynInput' },
      { label: this.$t('customer'), prop: 'customer', tag: 'DynInput' },
      { label: this.$t('packageType'), prop: 'packaingForm', tag: 'DynSelect' },
      { label: this.$t('createTime'), prop: '', tag: 'Dyndaterange', event: (value) => {
        this.infoForm.stratdate = Array.isArray(value) ? value[0] : null
        this.infoForm.enddate = Array.isArray(value) ? value[1] : null
      } }
    ]
    const tableColumns = [
      { prop: '', label: '', type: 'selection' },
      { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
        return (
          <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
        )
      } },
      { prop: 'customer', label: this.$t('customer') },
      { prop: 'productName', label: this.$t('productName') },
      { prop: 'processSpecificationNo', label: this.$t('processSpecificationNumber') },
      { prop: 'processFlow', label: this.$t('processNo') },
      { prop: 'packaingFormsCode', label: this.$t('packageType') },
      { prop: 'version', label: this.$t('version') },
      { prop: 'createTime', label: this.$t('createTime') },
      { prop: '', label: this.$t('details'), templete: (_scope) => {
        return (
          <el-link type='primary' underline={false}
            onClick={() => this.$refs.indexSetting.handleOpen(_scope.row, true)}
          >{this.$t('details')}</el-link>
        )
      } }
    ]
    return {
      infoForm: {},
      selectList: {},
      searchTags: Object.freeze(searchTags),
      tableColumns: Object.freeze(tableColumns),
      tableLoading: true,
      headers: {
        Token: getToken()
      }
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  created() {
    this.initList()
    this.getPackageTypes()
  },
  methods: {
    async initList() {
      const res = await getList(this.$clearNull({ ...this.listQuery, ...this.infoForm }))
      this.tableData = res.data
      this.total = res.total
      this.tableLoading = false
    },
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    doAdd() {
      this.$refs.indexSetting.handleOpen()
    },
    handleEdit() {
      this.$refs.indexSetting.handleOpen(this.multipleSelection[0])
    },
    handleDelete() {
      this.$confirm(
        `${this.$t('confirmDelete')}${this.multipleSelection.length}${this.$t(
          'recordCount'
        )}?`,
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map(ele => ele.id)
        doDelete({ ids }).then(res => {
          this.$betterTips(res, this.setOnePage, this.initList)
        })
      })
    },
    importFail() {
      this.$notify({
        message: this.$t('uploadFail'),
        type: 'error',
        duration: 3500
      })
    },
    uploadSuccess(response, file, fileList) {
      // 第{ele.index}行 {this.$t(ele.msgCode)}
      // 第{ele.split(',')[0]}行 {this.$t(ele.split(',')[1])}
      // this.netTips(response.code)
      if (response.code !== 1) {
        const h = this.$createElement
        const listVNode = response.data.map((item) => {
          return (
            <p style={ { width: '240px' }}>
              <span style={ { width: '100%', wordWrap: 'break-word' }}>{item.index ? `第{${item.index}}行` : ''}{this.$t(item.msgCode)}</span>
            </p>
          )
        })
        const styles = {
          color: 'teal',
          overflowX: 'hidden',
          overflowY: 'auto',
          width: '280px',
          maxHeight: 'calc(100vh - 120px)'
        }
        this.$notify({
          message: h('div', { style: styles }, listVNode),
          type: 'error',
          duration: 0
        })
        fileList = []
      } else {
        this.$betterTips(response, this.initList)
      }
    },
    onChangePage(page) {
      this.listQuery = page
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    async handleDownloadTemp() {
      const res = await download({ name: 'SBLSettingExcelList' })
      window.open(this.baseApi + '/' + res.data)
    },
    async getPackageTypes() {
      const res = await getPackageTypeList()
      this.$set(this.selectList, 'packaingForm', res.data)
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/defaultStyle.scss";
.upload-btn{
  margin: 0 10px;
}
</style>
